package dk.muldbjergskolen.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import dk.muldbjergskolen.ctr.LokaleCtr;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OpretLokale extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_Navn;
	private JTextField textField_Placering;
	private LokaleCtr lC = new LokaleCtr();
	private JFrame frame;

	/**
	 * Create the dialog.
	 */
	public OpretLokale(OpretLokaleListener listener) {
		opretLokaleListener = listener;
		
		setModalityType(ModalityType.APPLICATION_MODAL);
		setTitle("Opret Lokale");
		setBounds(100, 100, 201, 154);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Navn:");
		lblNewLabel.setBounds(10, 14, 65, 14);
		contentPanel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Placering:");
		lblNewLabel_1.setBounds(10, 39, 65, 14);
		contentPanel.add(lblNewLabel_1);
		
		textField_Navn = new JTextField();
		textField_Navn.setBounds(85, 11, 90, 20);
		contentPanel.add(textField_Navn);
		textField_Navn.setColumns(10);
		
		textField_Placering = new JTextField();
		textField_Placering.setBounds(85, 36, 90, 20);
		contentPanel.add(textField_Placering);
		textField_Placering.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						opret();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener()
				{
					public void actionPerformed(ActionEvent arg0)
					{
						setVisible(false);
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
		
		//
		// Event håndtering.
		//

		// "Lyttere"
		OpretLokaleListener opretLokaleListener;

		public interface OpretLokaleListener
		{
			void OnNewLokale();
		}

		/***
		 * Kaldes når en ny Lokale er lavet.
		 */
		private void OnNewLokale()
		{
			if (opretLokaleListener !=null)
				opretLokaleListener.OnNewLokale();
		}
		
		//opretter et lokale
		public void opret()
		{
			try
			{
			String navn = textField_Navn.getText();
			String placering = textField_Placering.getText();
			lC.opretLokale(navn, placering);
			}
			catch (Exception e)
			{
				JOptionPane.showMessageDialog(frame, "Udfyld alle Felterne");
				return;
			}
			OnNewLokale();
			setVisible(false);
		}
				
		
		
}
	
	


